/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       convert_16_bit.h

	$Header: /game/convert_16_bit.h $

	$NoKeywords: $

 ************************************************************************/

#if !defined(CONVERT_16_BIT_H_INCLUDED)
#define CONVERT_16_BIT_H_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "pixel_24.h"

extern int g_blue_16_bit_mask;         // blue bits
extern int g_blue_16_bit_mask_shifted; // same, shifted left 4 bits
extern int g_blue_16_bit_shift;
extern int g_green_16_bit_mask;         // green bits
extern int g_green_16_bit_mask_shifted; // same, shifted left 4 bits
extern int g_green_16_bit_shift;
extern int g_red_16_bit_mask;         // red bits
extern int g_red_16_bit_mask_shifted; // same, shifted left 4 bits
extern int g_red_16_bit_shift;
extern int g_alpha_16th_mask;
extern int g_alpha_8th_mask;
extern int g_alpha_4th_mask;
extern int g_alpha_half_mask;

extern int g_red_blue_16_bit_mask;
extern int g_red_blue_16_bit_mask_shifted;

extern int g_inverse_green_16_bit_mask;
extern int g_inverse_green_16_bit_mask_shifted;
extern int g_num_red_green_forward_shift;
extern int g_num_reg_green_backward_shift;
extern int g_green_red_blue_16_bit_mask;
extern int g_green_red_blue_16_bit_mask_shifted;

// assumes blue is the least significant bits.
inline t_uint16 convert_to_16_bit( t_pixel_24 const& pixel )
{
	return ((pixel.red   << g_red_16_bit_shift)   & g_red_16_bit_mask)
		 | ((pixel.green << g_green_16_bit_shift) & g_green_16_bit_mask)
		 |  (pixel.blue  >> g_blue_16_bit_shift);
}

// assumes blue is the least significant bits.
inline t_uint16 convert_to_16_bit( int red, int green, int blue )
{
	return ((red   << g_red_16_bit_shift)   & g_red_16_bit_mask)
		 | ((green << g_green_16_bit_shift) & g_green_16_bit_mask)
		 |  (blue  >> g_blue_16_bit_shift);
}

inline t_uint8 red_channel( t_uint16 pixel )
{
	return ( pixel & g_red_16_bit_mask ) >> g_red_16_bit_shift;
}

inline t_uint8 green_channel( t_uint16 pixel )
{
	return ( pixel & g_green_16_bit_mask ) >> g_green_16_bit_shift;
}

inline t_uint8 blue_channel( t_uint16 pixel )
{
	return ( pixel & g_blue_16_bit_mask ) << g_blue_16_bit_shift;
}

inline t_pixel_24 convert_to_24_bit( t_uint16 pixel )
{
	return t_pixel_24( red_channel( pixel ), green_channel( pixel ), blue_channel( pixel ) );
}

void set_pixel_masks( t_uint16 red_mask, t_uint16 green_mask, t_uint16 blue_mask );

// -------------------------------------------------------------------
// t_pixel_mask_viewer class - will be notified whenever pixel masks
// have changed
// -------------------------------------------------------------------

class t_pixel_mask_viewer
{
public:
	t_pixel_mask_viewer();
	virtual ~t_pixel_mask_viewer();

	virtual void on_pixel_masks_changed() = 0;
};

static int count_mask_bits16( int mask )
{
	int count = 0;

	for( int i = 0; i < 16; ++i )
	{
		if( mask & 0x01 )
			++count;

		mask = mask >> 1;
	}

	return count;
}

#endif // CONVERT_16_BIT_H_INCLUDED